package com.ruoyi.yljf.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.yljf.mapper.UserQuestionRecordMapper;
import com.ruoyi.yljf.domain.UserQuestionRecord;
import com.ruoyi.yljf.service.IUserQuestionRecordService;

import javax.annotation.Resource;

/**
 * 用户答题记录Service业务层处理
 * 
 * @author metaee
 * @date 2025-04-25
 */
@Service
public class UserQuestionRecordServiceImpl extends ServiceImpl<UserQuestionRecordMapper, UserQuestionRecord> implements IUserQuestionRecordService {
    @Resource
    private UserQuestionRecordMapper userQuestionRecordMapper;
    
    /**
     * 查询用户答题记录列表
     * 
     * @param userQuestionRecord 用户答题记录
     * @return 用户答题记录
     */
    @Override
    public List<UserQuestionRecord> selectUserQuestionRecordList(UserQuestionRecord userQuestionRecord) {
    	LambdaQueryWrapper<UserQuestionRecord> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotNull(userQuestionRecord.getUserId())) {
			queryWrapper.eq(UserQuestionRecord::getUserId, userQuestionRecord.getUserId());
		}
        if (StringUtils.isNotNull(userQuestionRecord.getCourseId())) {
			queryWrapper.eq(UserQuestionRecord::getCourseId, userQuestionRecord.getCourseId());
		}
        if (StringUtils.isNotNull(userQuestionRecord.getPackageId())) {
			queryWrapper.eq(UserQuestionRecord::getPackageId, userQuestionRecord.getPackageId());
		}
        if (StringUtils.isNotNull(userQuestionRecord.getQuestionId())) {
			queryWrapper.eq(UserQuestionRecord::getQuestionId, userQuestionRecord.getQuestionId());
		}
        if (StringUtils.isNotEmpty(userQuestionRecord.getUserOption())) {
			queryWrapper.eq(UserQuestionRecord::getUserOption, userQuestionRecord.getUserOption());
		}
        if (StringUtils.isNotEmpty(userQuestionRecord.getCorrectOption())) {
			queryWrapper.eq(UserQuestionRecord::getCorrectOption, userQuestionRecord.getCorrectOption());
		}
        return userQuestionRecordMapper.selectList(queryWrapper);
    }

    /**
     * 插入：用户章节试题单题提交记录
     * @param userQuestionRecord：用户章节试题单题提交记录
     */
    @Override
    public void saveUserQuestionRecord(UserQuestionRecord userQuestionRecord) {
        userQuestionRecordMapper.insert(userQuestionRecord);
    }

    /**
     * 课程考试单题答题记录提交
     * @param userQuestionRecord
     */
    @Override
    public void submitPaperQuestion(UserQuestionRecord userQuestionRecord) {
        UserQuestionRecord record = userQuestionRecordMapper.selectOne(new LambdaQueryWrapper<>(UserQuestionRecord.class)
                .eq(UserQuestionRecord::getUserId, SecurityUtils.getUserId())
                .eq(UserQuestionRecord::getCourseId, userQuestionRecord.getCourseId())
                .eq(UserQuestionRecord::getQuestionId, userQuestionRecord.getQuestionId()));
        if(record == null) {
            userQuestionRecordMapper.insert(userQuestionRecord);
        } else {
            record.setUserOption(userQuestionRecord.getUserOption());
            record.setCreateTime(new Date());
            userQuestionRecordMapper.updateById(record);
        }
    }
}
